package com.seaside.park.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seaside.common.bean.Result;
import com.seaside.park.bean.LabelValue;
import com.seaside.park.mapper.LabelValueMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "标签管理")
@RestController
@RequestMapping("/api/label_value")
public class LabelValueAPI {
    @Autowired
    LabelValueMapper labelValueMapper;

    @ApiOperation(value = "保存/修改标签", response = Result.class)
    @PostMapping("/save")
    public Result save(@RequestBody LabelValue labelValue) {
        if (labelValue.getId() == 0) {
            labelValueMapper.insert(labelValue);
        } else {
            labelValueMapper.updateById(labelValue);
        }
        return Result.OK("保存成功！", null);
    }

    @ApiOperation(value = "根据ID删除语音播报规则", response = Result.class)
    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        LabelValue labelValue = labelValueMapper.selectById(id);
        if (labelValue != null && labelValue.getId() > 0) {
            labelValueMapper.deleteById(labelValue);
        }
        return Result.OK("删除成功！", null);
    }

    @ApiOperation(value = "查询所有语音播报信息", response = Result.class)
    @GetMapping("/listAll")
    public Result listAll(@RequestParam(value = "curPage", defaultValue = "1") Integer curPage,
                          @RequestParam (value = "curSize", defaultValue = "10") Integer curSize) {
        Page<LabelValue> page = new Page<>(curPage, curSize);
        IPage<LabelValue> iPage = labelValueMapper.selectPage(page, new QueryWrapper<LabelValue>().orderByDesc("id"));
        return Result.OK("查询成功", iPage);
    }
}
